/*
 * GLFont.hpp
 *
 *  Created on: 01-12-2010
 *      Author: Przemek
 */

#pragma once

#include "geometry/Vector.hpp"
#include "geometry/Dimension.hpp"
#include "pch.hpp"

/*
 * Self drawing openGL image based font manager. Outputs the text with given font at the specified position.
 * Uses only the standard gl functions.
 */
class GLFont
{
	public:
		class LoadFontException: public std::runtime_error
		{
			public:
				LoadFontException(const std::string& additionalInfo);
		};

	private:
		struct FontChar
		{
				int width;
				int height;
				float tx1, ty1;
				float tx2, ty2;
		};

		struct Font
		{
				GLuint textureId;
				int textureWidth, textureHeight;
				int charsStartIndex, charsEndIndex;
				FontChar* chars;
		};

		Font font;

	public:
		~GLFont();

		void load(const std::string& filename, GLuint textureId);
		void begin() const;
		void drawTextAt(const std::string& text, const Vector& position) const;
		void end() const;
		Dimension measureTextSize(const std::string& text) const;

	private:
		void freeResources();
		void loadFontData(FILE* inputFile);
		char* loadTextureData(FILE* inputFile);
		void createOglTextureFromData(char* textureData);
		void calculateCharsSizes();

		const FontChar& getFontChar(const char c) const;
};

inline GLFont::LoadFontException::LoadFontException(const std::string& additionalInfo) :
	std::runtime_error("Cannot load font: " + additionalInfo)
{

}

